﻿Public Class ToolboxIconExtraction

    Friend Shared iconsDict As ResourceDictionary = Nothing

    Private Shared Function CreateDrawingImage(ByVal resourceName As String) As DrawingBrush
        Dim icoActivity = My.Application.Resources.Item(resourceName)
        Return New DrawingBrush() With {.Drawing = New ImageDrawing(icoActivity, New Rect(0, 0, 16, 16))}
    End Function

    Friend Shared Sub CreateInitialResource()
        '標準アクティビティのリソースを追加
        iconsDict = New ResourceDictionary With
                  {
                      .Source = New Uri("pack://application:,,,/System.Activities.Presentation;component/themes/icons.xaml")
                  }
        '独自追加アクティビティの登録
        iconsDict.Add("BookmarkActivityIcon", CreateDrawingImage("ActIcoBookmark"))
        iconsDict.Add("ReadKeyActivityIcon", CreateDrawingImage("ActIcoReadKey"))
        iconsDict.Add("CommentOutActivityIcon", CreateDrawingImage("ActIcoCommentout"))
        iconsDict.Add("MessageBoxActivityIcon", CreateDrawingImage("ActIcoMessageBox"))
        iconsDict.Add("ZipCompressActivityIcon", CreateDrawingImage("ActIcoZipFile"))

    End Sub

    Friend Overloads Shared Function ExtractIconResource(ByVal iconOrtypeName As String) As Object
        If iconsDict Is Nothing Then
            '標準アクティビティのリソースを追加
            CreateInitialResource()
        End If

        Dim resourceKey = GetResourceName(iconOrtypeName)
        Dim resource As Object = Nothing
        If iconsDict.Contains(resourceKey) Then resource = iconsDict(resourceKey)
        If Not TypeOf resource Is DrawingBrush Then
            resource = iconsDict("GenericLeafActivityIcon")
        End If

        Return resource
    End Function

    Friend Overloads Shared Function ExtractIconResource(ByVal targetType As Type) As Object
        Dim typeName = ""
        If targetType.IsGenericType Then
            typeName = targetType.GetGenericTypeDefinition.Name
        Else
            typeName = targetType.Name
        End If
        Return ExtractIconResource(typeName)
    End Function

    Friend Shared Function GetResourceName(ByVal typeName As String) As String
        Dim resourceKey = typeName
        resourceKey = resourceKey.Split(".").Last
        resourceKey = resourceKey.Split("`").First

        resourceKey = ConvertActivityName(resourceKey)
        If Not resourceKey.EndsWith("Icon") Then
            resourceKey += "Icon"
        End If
        Return resourceKey
    End Function

    Private Shared Function ConvertActivityName(ByVal name As String) As String
        Dim activityName = ""

        Select Case name
            Case "ForEachWithBodyFactory" : activityName = "ForEach"
            Case "If" : activityName = "If"
            Case "Parallel" : activityName = "Parallel"
            Case "ParallelForEachWithBodyFactory" : activityName = "ParallelForEach"
            Case "PickWithTwoBranchesFactory" : activityName = "Pick"
            Case "PickBranch" : activityName = "PickBranch"
            Case "Sequence" : activityName = "Sequence"
            Case "Switch" : activityName = "Switch"
            Case "While" : activityName = "While"
            Case "DoWhile" : activityName = "DoWhile"

            Case "Flowchart" : activityName = "FlowChart"
            Case "FlowDecision" : activityName = "FlowDecision"
            Case "FlowSwitch" : activityName = "FlowSwitch"

            Case "CorrelationScope" : activityName = "CorrelationScope"
            Case "InitializeCorrelation" : activityName = "InitializeCorrelation"
            Case "Receive" : activityName = "Receive"
            Case "ReceiveReply" : activityName = "ReceiveReply"
            Case "Presentation" : activityName = "Presentation"
            Case "Send" : activityName = "Send"
            Case "SendReply" : activityName = "SendReply"
            Case "Presentation" : activityName = "Presentation"
            Case "TransactedReceiveScope" : activityName = "TransactionReceiveScope"

            Case "Persist" : activityName = "Persist"
            Case "TerminateWorkflow" : activityName = "TerminateWorkflow"

            Case "Assign" : activityName = "Assign"
            Case "Delay" : activityName = "Delay"
            Case "InvokeMethod" : activityName = "InvokeMethod"
            Case "WriteLine" : activityName = "WriteLine"

            Case "CancellationScope" : activityName = "CancellationScope"
            Case "CompensableActivity" : activityName = "CompensableActivity"
            Case "Compensate" : activityName = "Compensate"
            Case "Confirm" : activityName = "Confirm"
            Case "TransactionScope" : activityName = "TransactionScope"

            Case "AddToCollection" : activityName = "AddToCollection"
            Case "ClearCollection" : activityName = "ClearCollection"
            Case "ExistsInCollection" : activityName = "ExistsInCollection"
            Case "RemoveFromCollection" : activityName = "RemoveFromCollection"

            Case "Rethrow" : activityName = "Rethrow"
            Case "Throw" : activityName = "Throw"
            Case "TryCatch" : activityName = "TryCatch"

            Case "Interop" : activityName = "Interop"

            Case "InvokeDelegate" : activityName = "InvokeDelegate"
            Case "NoPersistScope" : activityName = "NoPersistScope"

            Case Else : activityName = name
        End Select

        Return activityName
    End Function
End Class
